package com.heu.blood.storge.controller;

import com.heu.blood.common.aop.ControllerLogAnnotation;
import com.heu.blood.common.utils.PageUtils;
import com.heu.blood.common.utils.R;
import com.heu.blood.match.entity.BloodInformationEntity;
import com.heu.blood.match.service.BloodInformationService;
import com.heu.blood.storge.entity.BloodInStorgeEntity;
import com.heu.blood.storge.service.BloodInStorgeService;
import com.heu.blood.storge.vo.BloodDeleteReturnVo;
import com.heu.blood.storge.vo.BloodInformationAndInStorageVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 *
 *
 * @author lyj
 * @email lyj@heu.com
 * @date 2023-06-16 00:33:53
 */
@Slf4j
@RestController
@RequestMapping("storge/bloodinstorge")
public class BloodInStorgeController {
    @Autowired
    private BloodInStorgeService bloodInStorgeService;

    @Autowired
    private BloodInformationService bloodInformationService;
    /**
     * 列表
     */
    @ControllerLogAnnotation(module = "血液入库", operate = "分页查询血液入库信息")
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        log.info("params : {}", params); // 打印参数
        PageUtils page = bloodInStorgeService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @ControllerLogAnnotation(module = "血液入库", operate = "根据id查询血液入库信息")
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
        BloodInStorgeEntity bloodInStorge = bloodInStorgeService.getById(id);
        if(bloodInStorge == null) return R.error("不存在此血液信息，无法u获取");
        return R.ok().put("data", bloodInStorge);

    }

    /**
     * 保存
     */
    @ControllerLogAnnotation(module = "血液入库", operate = "插入血液入库信息(血液入库对象)")
    @RequestMapping("/save")
    public R save(@RequestBody BloodInStorgeEntity bloodInStorge){
        bloodInStorgeService.save(bloodInStorge);
        return R.ok();
    }

    /**
     *
     * @return
     */
    @ControllerLogAnnotation(module = "血液入库", operate = "批量插入血液入库信息需要进行检验")
    @RequestMapping("/batchInsertWithVerify")
    public R batchInsertWithVerify(@RequestBody List<BloodInStorgeEntity> bloodInStorgeEntityList){
        //改为不同时向血液信息表插入
       if(bloodInStorgeService.batchInsertWithVerify(bloodInStorgeEntityList)){
           return R.ok("批量入库成功");
       }else {
           return R.ok("批量入库失败");
       }
    }



    /**
     * 修改
     */
    @ControllerLogAnnotation(module = "血液入库", operate = "同步更新血液入库信息")
    @RequestMapping("/update")
    public R update(@RequestBody BloodInStorgeEntity bloodInStorge){
        bloodInStorgeService.updateById(bloodInStorge);
        //同步更新血液信息bloodinformation表
        //判定是否更新
        bloodInStorgeService.updateBloodInformationByBloodId(bloodInStorge);
        if(bloodInStorgeService.updateBloodInformationByBloodId(bloodInStorge)){
            return R.ok();
        }
        return R.ok();
    }

    /**
     * 删除
     */
    @ControllerLogAnnotation(module = "血液入库", operate = "根据id删除血液入库信息")
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
        bloodInStorgeService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 这个是实际用的
     */
    @ControllerLogAnnotation(module = "血液入库", operate = "根据bloodId级联删除血液入库和血液信息表数据")
    @PostMapping("/cascadeDelete")
    public R cascadeDelete(@RequestBody BloodDeleteReturnVo vo){
        int res= bloodInStorgeService.cascadeDelete(vo);
        return R.ok();
    }
//    @ControllerLogAnnotation(module = "血液入库", operate = "根据bloodId级联删除血液入库和血液信息表数据")
//    @RequestMapping("/cascadeDelete")
//    public R cascadeDelete(@RequestBody List<String> bloodIds){
//       int res= bloodInStorgeService.cascadeDelete(bloodIds);
//        return R.ok();
//    }


    @ControllerLogAnnotation(module = "血液入库", operate = "插入一条血液入库信息(血液信息对象)")
    @RequestMapping("/inStorge")
    public R inStorge(@RequestBody BloodInStorgeEntity bloodInStorgeEntity){

        int res = bloodInStorgeService.insertInStorge(bloodInStorgeEntity);
        log.info("========================================");
        log.info(String.valueOf(res));
        return R.ok();
    }
    @ControllerLogAnnotation(module = "血液入库", operate = "弃用的分页查询接口")
    @RequestMapping("/PageQueryForInStorge")
    public R PageQueryForInStorge(@RequestParam Map<String, Object> params){
        log.info("params : {}", params); // 打印参数
        PageUtils page = bloodInStorgeService.queryPage(params);
        return R.ok().put("page", page);

    }

    //excel批量导入到数据库表中
    @ControllerLogAnnotation(module = "血液入库", operate = "弃用的根据excel批量导入入库信息")
    @RequestMapping("/uploadExcel")
    public R uploadExcel(@RequestParam("file") MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename(); // 获取文件名

        InputStream is = null;

        try{
            is = file.getInputStream();
            List<Map> bloodInformationList = bloodInStorgeService.getListByExcel(is,fileName);// 获取解析后的List集合
            // System.out.println(studentList.toString());
            Boolean result = bloodInStorgeService.batchImportBloodInformationInfo(bloodInformationList); // 把数据插入数据库
            if (result){
                return R.ok("文件上传成功！");
            }else {
                return R.ok( "文件上传失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
        } finally {
            is.close();
        }
        return R.error("文件错误！");
    }

    @ControllerLogAnnotation(module = "血液入库", operate = "根据excel批量导入入库信息")
    @PostMapping("/uploadExcelForBloodInstorge")
    public R importExcel(@RequestParam("file") MultipartFile multipartFile){
        System.out.println("--------------");
        System.out.println(multipartFile);
        bloodInStorgeService.importExcel(multipartFile);
        //bloodInformationService.importExcel(multipartFile);
        return R.ok("ok");
    }

    /**
     * 新增前端传递blood_id 向血液信息表里面塞入这条血液信息 同时更改两个在库状态
     */
    @ControllerLogAnnotation(module = "血液入库", operate = "根据血液id向血液信息表里面塞入这条血液信息,同时更改状态")
    @RequestMapping("/insertOneInformationToBloodInformationFromBloodInStorgeByBloodId")
    public R insertOneInformationToBloodInformationFromBloodInStorgeByBloodId(String bloodId){
        System.out.println("------------------------------test--");
        bloodInStorgeService.insertOneInformationToBloodInformationFromBloodInStorgeByBloodId(bloodId);
        return R.ok("ok");
    }
    /**
     * 新增前端传递血液id数据 向血液信息表里面批量插入血液信息 同时更改在库状态333
     */
    @ControllerLogAnnotation(module = "血液入库", operate = "根据血液id数组向血液信息表里面批量插入血液信息,同时更改状态")
    @RequestMapping("/InsertMultiplyInformationToBloodInformationFromBloodInStorgeByBLoodId")
    public R InsertMultiplyInformationToBloodInformationFromBloodInStorgeByBLoodId(@RequestBody String[] bloodIds){
        log.info("------------"+bloodIds);
        System.out.println(bloodIds);
        bloodInStorgeService.InsertMultiplyInformationToBloodInformationFromBloodInStorgeByBLoodId(bloodIds);
        return R.ok("ok");
    }

    /**
     * 避免检验，直接进行单条血液入库，并更新为血液在库状况
     * @param bloodInformationAndInStorageVo
     * @return 成功标志
     */
    @ControllerLogAnnotation(module = "血液入库", operate = "避免检验，直接进行单条血液入库，并更新为血液在库状况")
    @RequestMapping("/insertWithoutVerify")
    public R insertWithoutVerify(@RequestBody BloodInformationAndInStorageVo bloodInformationAndInStorageVo){
        int result =  bloodInStorgeService.insertWithoutVerify(bloodInformationAndInStorageVo);
        if(result == 0){
            return R.error("未能正确同步更新成功");
        }else{
            return R.ok();
        }
    }

    /**
     * 避免检验，直接进行批量血液入库，并更新为血液在库状况
     * @param bloodInformationAndInStorageVos
     * @return
     */
    @ControllerLogAnnotation(module = "血液入库", operate = "批量插入血液入库信息")
    @RequestMapping("/batchInsertWithoutVerify")
    public R batchInsertWithoutVerify(@RequestBody List<BloodInformationAndInStorageVo> bloodInformationAndInStorageVos){
        //改为不同时向血液信息表插入
        int result = bloodInStorgeService.batchInsertWithoutVerify(bloodInformationAndInStorageVos);
        if(result == 0){
            return R.error("未能正确同步更新成功");
        }else{
            return R.ok();
        }
    }

    @GetMapping("/queryStorageByBloodId")
    @ControllerLogAnnotation(module = "血液入库", operate = "回显血液入库信息")
    public R queryStorageByBloodId(String bloodId){
        BloodInStorgeEntity bloodInStorgeEntity = bloodInStorgeService.queryStorageByBloodId(bloodId);
        return R.ok().put("data", bloodInStorgeEntity);
    }
}



